package com.yzc.role;

import com.yzc.db.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RoleDao {

    @Autowired
    private SpringDB springDB;

    /**
     * 获取用户数据
     *
     * @return
     */
    public List loadaAll() {
        String sql = "select * from t_role where state=0";
        System.out.println(sql);
        return springDB.getJdbcTemplate().queryForList(sql);
    }

    public Map save(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            String sql = "insert into t_role values(0,'" + roleBean.getName() + "','" + roleBean.getMemo() + "',0)";
            springDB.getJdbcTemplate().execute(sql);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            e.printStackTrace();
        }
        return map;
    }

    public Map deleteInfo(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            String sql = "update t_role set state=1 where id=" + roleBean.getId();
            springDB.getJdbcTemplate().execute(sql);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            e.printStackTrace();
        }
        return map;
    }

    public List selectInfos(RoleBean roleBean) {
        String sql = "select * from t_role where name like '%" + roleBean.getName() + "%' and state=0";
        return springDB.getJdbcTemplate().queryForList(sql);
    }

    public Map getInfoById(RoleBean roleBean) {
        String sql = "select * from t_role where id=" + roleBean.getId();
        return springDB.getJdbcTemplate().queryForMap(sql);
    }

    public Map updateInfo(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            String sql = "update t_role set name='" + roleBean.getName() + "',memo='" + roleBean.getMemo() + "' where id=" + roleBean.getId();
            springDB.getJdbcTemplate().execute(sql);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }


    public void deleteMenus(RoleBean roleBean) {
        String sql = "delete from t_role_menu where roleid=" + roleBean.getId();
        springDB.getJdbcTemplate().execute(sql);
    }

    public void saveMenus(RoleBean roleBean) {
        int[] permMenus = roleBean.getPermMenus();
        for (int i = 0; i < permMenus.length; i++) {
            String sql = "insert into t_role_menu values(0," + roleBean.getId() + "," + permMenus[i] + ")";
            System.out.println(sql);
            springDB.getJdbcTemplate().execute(sql);
        }
    }


    public List loadRoleList() {
        String sql = "select * from t_role where state=0";
        return springDB.getJdbcTemplate().queryForList(sql);
    }
}
